package List;

import java.util.LinkedList;
import java.util.List;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/4/28
 *  练习1：用LinkedList集合，模拟队列操作(存入的元素是Person对象)
 *  练习2：用LinkedList集合，模拟堆栈操作(存入的元素是Person对象)
 */
class Stack {
    private LinkedList<Person1> list = new LinkedList<Person1>();
    //初始化栈
    void InitStack(){
        list.clear();
    }
    //判栈空
    boolean StackEmpty(){
        if(list.isEmpty()){
            return true;
        }else{
            return false;
        }
    }
    //进栈
    void Push(Person1 p){
        list.addLast(p);
    }
    //读栈顶元素
    Person1 GetTop(){
        if(StackEmpty()){
            return null;
        }
        return list.getLast();
    }
    //出栈
    void Pop(){
        if(StackEmpty()){
            return;
        }
        list.removeLast();
    }
    void Print(){
        System.out.println(list);
    }
}
class Person1{
    private int age;
    private String name;
    public Person1() {
    }
    public Person1(int age, String name) {
        this.age = age;
        this.name = name;
    }

    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }

    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        this.age = age;
    }

    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }

    /**
     * 设置
     * @param name
     */
    public void setName(String name) {
        this.name = name;
    }

    public String toString() {
        return "Person1{age = " + age + ", name = " + name + "}";
    }
}
public class Test{
    public static void main(String[] args) {
        Person1 p1 = new Person1(19,"张三");
        Person1 p2 = new Person1(45,"李四");
        Person1 p3 = new Person1(27,"王五");
        Person1 p4 = new Person1(37,"赵六");
        Person1 p5 = new Person1(46,"钱七");
        Stack stack = new Stack();
        stack.InitStack();
        System.out.println("当前的栈是否为空："+stack.StackEmpty());
        stack.Push(p1);
        stack.Push(p2);
        stack.Push(p3);
        stack.Push(p4);
        stack.Push(p5);
        stack.Print();
        stack.Pop();
        stack.Print();
        System.out.println("此时的栈顶元素是："+stack.GetTop());


    }
}
